# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#   
#   cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
#   Major.create(:name => 'Daley', :city => cities.first)
users = User.create([
	{:name => 'Admin', :user_name => 'admin', :password => '123456', :role => 'admin'},
	{:name => 'Nguyen Hue Minh', :user_name => 'minhnh', :password => '123456', :role => 'teacher', :point => 0},
	{:name => 'Nguyen Thanh Binh',:user_name => 'binhnt', :password => '123456', :role => 'teacher', :point => 0},
	{:name => 'Bui Khac Thach',:user_name => 'thachbk', :password => '123456', :role => 'student', :point => 100},
	{:name => 'Tran Xuan Chien',:user_name => 'chientx', :password => '123456', :role => 'student', :point => 100},
])

grades = Grade.create([
	{:name => 'Lớp 6'},
	{:name => 'Lớp 7'},
	{:name => 'Lớp 8'},
	{:name => 'Lớp 9'},

	{:name => 'Lớp 10'},
	{:name => 'Lớp 11'},
	{:name => 'Lớp 12'}
])

subjects = Subject.create([
	{:name => 'Toán'},
	{:name => 'Lý'},
	{:name => 'Hóa'},
	{:name => 'Sinh'},
	{:name => 'Tiếng Anh'},
	{:name => 'Tiếng Pháp'},
	{:name => 'Tiếng Nga'}
])

query = []
users.each do |user|
	next if user.role != 'teacher'	

	grades.each do |grade|
		query << "INSERT INTO grades_users(grade_id,user_id) VALUES(#{grade.id},#{user.id})"
	end
	
	subjects.each do |subject|
		query << "INSERT INTO subjects_users(subject_id,user_id) VALUES(#{subject.id},#{user.id})"
	end		
end	

Subject.find_by_sql(query.join(';'))




